#include "speedpanel.h"
#include <QPainter>
#include <QDebug>

SpeedPanel::SpeedPanel(QWidget *parent) : QWidget(parent)
{
    m_sideColor = QColor(78,78,78);
    m_bgColor = QColor(61,61,61);
    m_scaleColor = QColor(254,254,254);

    m_startAngle = 210; //开始角度 210-240
    m_spanAngle = 240;  //角度跨度

    m_value = 44;
    m_minValue  = 0;
    m_maxValue = 100;
    m_threashold1 = 60;
    m_threashold2 = 80;

    m_maxSteps = 10;
    m_minStepsPerMaxStep = 10;
}

void SpeedPanel::paintEvent(QPaintEvent *)
{
    int side = qMin(width(), height());

    QPainter painter;
    painter.begin(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::TextAntialiasing, true);
    painter.translate(width()/2, height()/2);
    painter.scale(side/220.0, side/220.0);

    //绘制边框圆
    drawSideCircle(&painter);

    //绘制背景圆
    drawBgCircle(&painter);

    //绘制刻度
    drawScale(&painter);

    //绘制内部弧形
    drawInSideArc(&painter);

    //绘制指针
    drawValueHand(&painter);

    painter.end();
}

QSize SpeedPanel::sizeHint() const
{
    return QSize(220, 220);
}

void SpeedPanel::setOutSideColor(QColor color)
{
    m_sideColor = color;
}

void SpeedPanel::setBgColor(QColor color)
{
    m_bgColor = color;
}

void SpeedPanel::setScalColor(QColor color)
{
    m_scaleColor = color;
}

void SpeedPanel::setInsideColor(QColor passColor, QColor warnColor, QColor failColor)
{
    m_passColor = passColor;
    m_warnColor = warnColor;
    m_failColor = failColor;
}

void SpeedPanel::setHandColor(QColor color)
{
    m_HandColor = color;
}

void SpeedPanel::setValuColor(QColor bgColor, QColor textColor)
{
    m_valueBgColor = bgColor;
    m_valueTextColor = textColor;
}

void SpeedPanel::setRange(double max, double min)
{
    m_maxValue = max;
    m_minValue = min;
}

void SpeedPanel::setTh1Value(double th1)
{
    m_threashold1 = th1;
}

void SpeedPanel::setTh2Value(double th2)
{
    m_threashold2 = th2;
}

void SpeedPanel::setMaxSteps(int count)
{
    m_maxSteps = count;
}

void SpeedPanel::setMinStepsPerMax(int count)
{
    m_minStepsPerMaxStep = count;
}

void SpeedPanel::setValue(double value)
{
    m_value = value;
    this->update();
}

void SpeedPanel::setDicimalPrecision(int precision)
{
    m_precision = precision;
}

//绘制边框
void SpeedPanel::drawSideCircle(QPainter *painter)
{
    int radius = 99;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(m_sideColor);
    painter->drawEllipse(QPointF(0,0), radius, radius);
    painter->restore();
}

//绘制背景
void SpeedPanel::drawBgCircle(QPainter *painter)
{
    int radius = 90;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(m_bgColor);
    painter->drawEllipse(QPointF(0,0), radius, radius);
    painter->restore();
}

//绘制刻度
void SpeedPanel::drawScale(QPainter *painter)
{
    int steps = m_maxSteps*m_minStepsPerMaxStep;
    double angleStep =  static_cast<double>(m_spanAngle)/steps;
    int valueStep = static_cast<int>(m_maxValue-m_minValue)/m_maxSteps;

    int radius = 75;
    painter->save();
    QPen pen;
    pen.setColor(m_scaleColor);
    pen.setCapStyle(Qt::RoundCap);
    pen.setWidth(4);
    painter->setPen(pen);
    painter->rotate(90);
    painter->rotate(-m_startAngle);

    int value = static_cast<int>(m_minValue);

    for (int i=0; i<=steps; i++)
    {
        if(i%m_minStepsPerMaxStep == 0)
        {
            pen.setWidth(2);
            painter->setPen(pen);
            painter->drawLine(0, -1*(radius-10), 0, -1*radius);

            QString text = QString::number(value);
            value += valueStep;
            QFontMetrics fm = painter->fontMetrics();
            QPointF pt(-1*fm.width(text)/2, -1*radius-2-fm.descent());

            painter->drawText(pt, text);
        }
        else
        {
            pen.setWidth(1);
            painter->setPen(pen);
            painter->drawLine(0, -1*(radius-5), 0, -radius);
        }
        painter->rotate(angleStep);
    }

    painter->restore();
}

//绘制内部
void SpeedPanel::drawInSideArc(QPainter *painter)
{
    painter->save();

    double startAngle = m_startAngle*16;
    double spanAngle = m_spanAngle*16;
    double th1Angle = m_spanAngle*16.0*(m_threashold1-m_minValue)/(m_maxValue-m_minValue);
    double th2Angle = m_spanAngle*16.0*(m_threashold2-m_threashold1)/(m_maxValue-m_minValue);

    int radius = 58;
    QRectF rectangle(-radius, -radius, radius*2, radius*2);

    QPen pen;
    pen.setWidth(10);
    pen.setCapStyle(Qt::FlatCap);

    pen.setColor(QColor(23,187,153));
    painter->setPen(pen);
    painter->drawArc(rectangle,
                     static_cast<int>(startAngle),
                     -static_cast<int>(th1Angle) );
    startAngle -= th1Angle;

    pen.setColor(QColor(219,220,2));
    painter->setPen(pen);
    painter->drawArc(rectangle,
                     static_cast<int>(startAngle),
                     -static_cast<int>(th2Angle));
    startAngle -= th2Angle;

    pen.setColor(QColor(252,110,108));
    painter->setPen(pen);
    painter->drawArc(rectangle,
                     static_cast<int>(startAngle),
                     -static_cast<int>(spanAngle-th1Angle-th2Angle));

    painter->restore();
}

// 显示指针
void SpeedPanel::drawValueHand(QPainter *painter)
{
    double angle = m_startAngle-m_spanAngle*(m_value-m_minValue)/(m_maxValue-m_minValue);

    //绘制指针
    painter->save();
    painter->rotate(90);
    painter->rotate(-angle);
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(227,103,111));

    QPointF points[3] = {};
    points[0] = QPointF(-5, 0);
    points[1] = QPointF(5, 0);
    points[2] = QPointF(0, -72);

    painter->drawPolygon(points, 3);
    painter->drawEllipse(QPointF(0,0), 10, 10);
    painter->setBrush(QColor(200,200,200));
    painter->drawEllipse(QPointF(0,0), 7, 7);

    painter->restore();

    // 绘制数值
    QRect rect(-35, 40, 70, 25);
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(100,100,100));
    painter->drawRect(rect);

    QString text = QString::number(m_value, 'f', 1);

    QPen pen;
    pen.setColor(Qt::white);
    pen.setWidth(2);
    QFont font;
    font.setPointSize(16);
    painter->setFont(font);
    painter->setPen(pen);
    painter->drawText(rect,Qt::AlignCenter, text);

    painter->restore();
}
